package com.chuisha.stu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chuisha.stu.service.AccountService;
import com.chuisha.stu.service.dao.AccountDao;
import com.chuisha.stu.service.dao.AccountEntity;
import com.chuisha.stu.service.vo.AccountVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountDao accountDao;

    public AccountVO selectByCode(String accountCode) {
        AccountEntity entity = new AccountEntity();
        entity.setAccountCode(accountCode);
        entity = accountDao.selectOne(new QueryWrapper<>(entity));
        if (entity == null) {
            return null;
        }
        AccountVO accountVO = new AccountVO();
        BeanUtils.copyProperties(entity, accountVO);
        return accountVO;
    }

    public AccountVO updateAccount(AccountVO accountVO) {
        AccountEntity entity = new AccountEntity();
        BeanUtils.copyProperties(accountVO, entity);
        accountDao.updateById(entity);
        return new AccountVO();
    }

    public AccountVO insertAccount(AccountVO accountVO) {

        AccountEntity entity = new AccountEntity();
        BeanUtils.copyProperties(accountVO, entity);
        accountDao.insert(entity);
        accountVO.setId(entity.getId());
        return accountVO;
    }

    public int deleteAccount(String accountCode) {
        AccountEntity entity = new AccountEntity();
        entity.setAccountCode(accountCode);
        return accountDao.delete(new QueryWrapper<>(entity));
    }
}
